import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'one.dart';
import 'keybean.dart';
import 'test.dart';
import 'two.dart';

void main() {
  runApp(MyApp());
  const bool kIsWeb = identical(0, 0.0);
  if (kIsWeb) {
    KeyBean.isWeb = true;
  } else {
    KeyBean.isWeb = false;
    if (Platform.isAndroid) {
      SystemUiOverlayStyle style =
          SystemUiOverlayStyle(statusBarColor: Colors.transparent);
      SystemChrome.setSystemUIOverlayStyle(style);
    }
  }
  print(KeyBean.isWeb);
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '投票系统',
      theme: ThemeData(
        primarySwatch: Colors.indigo,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: '投票系统'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  TabController controller;
  int index = 0;

  @override
  void initState() {
    super.initState();
    controller = TabController(length: 3, vsync: this);
    controller.addListener(() {
      if (!controller.indexIsChanging) {
        setState(() {
          index = controller.index;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    KeyBean.width = size.width;
    KeyBean.height = size.height;

    var dh = KeyBean.height / 16;
    if (KeyBean.width > dh * 10) {
      KeyBean.width = dh * 9;
    }

    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: TabBarView(
        controller: controller,
        children: <Widget>[OnePage(), TwoPage(), TestPage()],
      ),
      bottomNavigationBar: TabBar(
          controller: controller,
          labelColor: Colors.indigo,
          unselectedLabelColor: Colors.black87,
          labelPadding: EdgeInsets.all(0),
          tabs: <Widget>[
            Tab(
              child: Container(
                color: index == 0 ? Colors.indigo : Colors.white,
                alignment: Alignment.center,
                width: double.infinity,
                height: 50,
                child: Text("全部参赛",
                    style: TextStyle(
                        color: index == 0 ? Colors.white : Colors.black87,
                        fontSize: index == 0 ? 18 : 16)),
              ),
            ),
            Tab(
              child: Container(
                color: index == 1 ? Colors.indigo : Colors.white,
                width: double.infinity,
                alignment: Alignment.center,
                height: 50,
                child: Text("当前排名",
                    style: TextStyle(
                        color: index == 1 ? Colors.white : Colors.black87,
                        fontSize: index == 1 ? 18 : 16)),
              ),
            ),
            Tab(
              child: Container(
                color: index == 2 ? Colors.indigo : Colors.white,
                width: double.infinity,
                height: 50,
                alignment: Alignment.center,
                child: Text("活动介绍",
                    style: TextStyle(
                        color: index == 2 ? Colors.white : Colors.black87,
                        fontSize: index == 2 ? 18 : 16)),
              ),
            ),
          ]), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
